package me.spring.cloud.components.starter.business.log.dynamic;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Set;

/**
 * @author YuanWei
 * @description: TODO
 * @date 2019/3/29 11:36
 */
@Slf4j
public class DynamicBusLogConfiguration implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @ApolloConfigChangeListener
    private void onChange(ConfigChangeEvent changeEvent) {
        refreshProperties(changeEvent.changedKeys());
    }

    private void refreshProperties(Set<String> changedKeys) {
        log.info("Refreshing business log : {}", changedKeys);
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changedKeys));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
